home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / net / tf02.lha / TinyFugue / tf-lib / factoral.tf < prev    next >
Text File  |  1995-08-12  |  688b  |  29 lines

  1. ;;;; factorial macros
  2. ;;; Not very useful, but they do demonstrate some macro programming techniques.
  3.  
  4. ;; recursive factorial
  5.  
  6. /def rfact = \
  7.     /if ( {1} < 0 ) \
  8.         /echo %% factorial: negative argument%; \
  9.     /elseif ( {1} == 0 ) \
  10.         /echo 1%; \
  11.     /else \
  12.         /eval /echo -- $$[{1} * $(/rfact $[{1} - 1])]%; \
  13.     /endif
  14.  
  15. ;; iterative factorial - more efficient
  16.  
  17. /def ifact = \
  18.     /if ( {1} < 0 ) \
  19.         /echo %% factorial: negative argument%; \
  20.     /else \
  21.         /let n=%1%; \
  22.         /let result=1%; \
  23.         /while (n) \
  24.             /let result=$[result * n]%; \
  25.             /let n=$[n - 1]%; \
  26.         /done%; \
  27.         /echo -- %result%; \
  28.     /endif%; \
  29.